package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.dto.criteria.MerchantBatchPayFileCriteria;
import com.ayf.payment.game.api.entity.MerchantBatchPayFile;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.MerchantBatchPayFileMapper;
import com.ayf.payment.game.api.service.MerchantBatchPayFileService;
import com.ayf.payment.game.api.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MerchantBatchPayFileServiceImpl implements MerchantBatchPayFileService {

    @Resource
    private MerchantBatchPayFileMapper merchantBatchPayFileMapper;

    @Override
    public boolean add(MerchantBatchPayFile merchantBatchPayFile) {
        if (StringUtils.isBlank(merchantBatchPayFile.getId())) {
            throw new ParameterException("代付文件id不能为空");
        }
        if (merchantBatchPayFile.getMerchantId() == null) {
            throw new ParameterException("商户id不能为空");
        }
        return merchantBatchPayFileMapper.insertSelective(merchantBatchPayFile) > 0;
    }

    @Override
    public boolean updatePayFile(MerchantBatchPayFile merchantBatchPayFile) {
        if (StringUtils.isBlank(merchantBatchPayFile.getId())) {
            throw new ParameterException("代付文件id不能为空");
        }
        return merchantBatchPayFileMapper.updateByPrimaryKeySelective(merchantBatchPayFile) > 0;
    }

    @Override
    public List<MerchantBatchPayFile> selectList(MerchantBatchPayFileCriteria criteria) {
        if (StringUtils.isNotBlank(criteria.getStartTime())) {
            Date date = DateUtils.stringToDate(criteria.getStartTime());
            criteria.setStartTimeIndex(DateUtils.dateToInteger(date));
        }
        if (StringUtils.isNotBlank(criteria.getEndTime())) {
            Date date = DateUtils.stringToDate(criteria.getEndTime());
            criteria.setEndTimeIndex(DateUtils.dateToInteger(date));
        }
        return merchantBatchPayFileMapper.selectList(criteria);
    }
}

